package com.dlwx.firefighting.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.AndroidShare;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.views.CustomDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.autolayout.AutoLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserQrCodeActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.img_create_qicode)
    ImageView img_code;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.img_qrcode_re)
    ImageView imgQrcodeRe;
    @BindView(R.id.al_qr)
    AutoLinearLayout alQr;
    private String userId;
    private CustomDialog mCustomDialog;
    private ArrayAdapter<String> adapter;
    private Bitmap shareBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_qr_code);
        ButterKnife.bind(this);
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
        initView();
        getDatas();
        applayForQrCode();
    }

    private void getDatas() {
        OkGo.<String>post(Constant.QRCODE)
                .params("user_id", userId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        List<StatusBean> beans = dealResult.deal(UserQrCodeActivity.this, response, new TypeToken<BaseBean<StatusBean>>() {
//                        }.getType());
//                        if (beans.isEmpty()) {
//                            return;
//                        }
//                        Glide.with(UserQrCodeActivity.this).load(beans.get(0).getQrcode()).asBitmap().into(new SimpleTarget<Bitmap>() {
//                            @Override
//                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                                shareBitmap = resource;
//                                img_code.setImageBitmap(shareBitmap);
//
//                            }
//                        });
                    }
                });
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        titleRightOneTv.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("我的二维码");
        titleRightOneTv.setText("申请扫码权限");
        img_code.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showDialog();
                return true;
            }
        });
    }

    /**
     * 显示Dialog
     * param v
     */
    private void showDialog() {
        initAdapter();
        mCustomDialog = new CustomDialog(this) {
            @Override
            public void initViews() {
                // 初始CustomDialog化控件
                ListView mListView = (ListView) findViewById(R.id.lv_dialog);
                mListView.setAdapter(adapter);
                mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // 点击事件
                        AndroidShare share = new AndroidShare(UserQrCodeActivity.this);
                        switch (position) {
                            case 0:
                                share.shareWeChatFriend("", "", 1, shareBitmap);
//								sendToFriends();//把图片发送给好友
                                closeDialog();
                                break;
                            case 1:
                                share.shareQQFriend("", "", 1, shareBitmap);
//								sendToFriends();//把图片发送给好友
                                closeDialog();
                                break;
                            case 2:
                                saveImageToGallery(UserQrCodeActivity.this);
                                closeDialog();
                                break;
                            case 3:
//                                goIntent();
                                closeDialog();
                                break;
                        }
                    }
                });
            }
        };
        mCustomDialog.show();
    }

    /**
     * 初始化数据
     */
    private void initAdapter() {
        adapter = new ArrayAdapter<String>(this, R.layout.item_dialog);
        adapter.add("分享到微信");
        adapter.add("分享到QQ");
//        adapter.add("保存到手机");
        /*adapter.add("收藏");*/
    }

    /**
     * 先保存到本地再广播到图库
     */
    public void saveImageToGallery(Context context) {

    /*    // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), "code", null);
            // 最后通知图库更新
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"
                    + file)));
            Toast.makeText(UserQrCodeActivity.this, "图片保存成功", Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
    }


    @OnClick({R.id.title_left_one_btn, R.id.title_right_one_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.title_right_one_tv:
                applayForQrCode();
                break;
        }
    }

    private void applayForQrCode() {
        OkGo.<String>post(Constant.TRANSFER_ACCOUNTS_QRCODE)
                .params("user_id", userId)
//                .params("transfer_accounts_status", 0)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        List<StatusBean> beans = dealResult.deal(UserQrCodeActivity.this, response, new TypeToken<BaseBean<StatusBean>>() {
//                        }.getType());
//                        if (beans.isEmpty()) {
//                            return;
//                        }
//
//                        alQr.setVisibility(View.VISIBLE);
//                        SPUtils.put(UserQrCodeActivity.this, Constant.IS_SCAN, true);
//                        Glide.with(UserQrCodeActivity.this).load(beans.get(0).getQrcode()).asBitmap().into(new SimpleTarget<Bitmap>() {
//                            @Override
//                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                                shareBitmap = resource;
//                                imgQrcodeRe.setImageBitmap(shareBitmap);
//                            }
//                        });
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
    }
}
